﻿using EFModel;
using IServices;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xm.Controllers;

namespace Serives
{
    public class Tesalary_grant : Isalary_grant
    {
        private readonly MyDBContext he;

        public Tesalary_grant(MyDBContext he)
        {
            this.he = he;
        }
        //人力资源未发工资查询
        public Task<ResultInfo> FenY(int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.human_file
                .Where(a=> a.human_file_status == 0)
                    .GroupBy(e => new { e.first_kind_id, e.first_kind_name, e.second_kind_id, e.second_kind_name, e.third_kind_id, e.third_kind_name })
                    .Select(s => new { s.Key.first_kind_id, s.Key.first_kind_name, s.Key.second_kind_id, s.Key.second_kind_name, s.Key.third_kind_id, s.Key.third_kind_name, SUM = s.Sum(t => t.salary_sum), Count = s.Count() })
                    .ToList();
                var list = he.human_file
                   .Where(a => a.human_file_status == 0)
                    .GroupBy(e => new { e.first_kind_id, e.first_kind_name, e.second_kind_id, e.second_kind_name, e.third_kind_id, e.third_kind_name })
                    .Select(s => new { s.Key.first_kind_id, s.Key.first_kind_name, s.Key.second_kind_id, s.Key.second_kind_name, s.Key.third_kind_id, s.Key.third_kind_name,salary_standard_sum=s.Sum(t=> t.salary_sum), human_amount = s.Count() })
                    .OrderBy(q => q.third_kind_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //按三级机构查人力资源
        public Task<ResultInfo> FenYTo(human_file _File)
        {
            return Task.Run(() => {
                var con = he.human_file.Where(e=> e.third_kind_id == _File.third_kind_id);
                var sum = he.human_file.Where(e => e.third_kind_id == _File.third_kind_id)
                .Sum(s=> s.salary_sum);
                var list = he.human_file
                    .Where(e => e.third_kind_id == _File.third_kind_id&&e.human_file_status == 0)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    sum = (int)sum,
                    Row = cont
                };
                return result;
            });
        }
        //按三级机构查人力资源未发工资
        public Task<ResultInfo> FenYTos(human_file _File)
        {
            return Task.Run(() => {
                var con = he.human_file.Where(e => e.third_kind_id == _File.third_kind_id && e.human_file_status!=20);
                var sum = he.human_file.Where(e => e.third_kind_id == _File.third_kind_id && e.human_file_status != 20)
                .Sum(s => s.salary_sum);
                var list = he.human_file
                    .Where(e => e.third_kind_id == _File.third_kind_id)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    sum = (int)sum,
                    Row = cont
                };
                return result;
            });
        }
        //按薪酬编号查薪酬详细信息
        public Task<string> Que(string id)
        {
            return Task.Run(() => {             
                return JsonConvert.SerializeObject(he.salary_standard_details.Where(e => e.standard_id == id));
            });
        }
        //人力资源状态修改(已发工资)
        public Task<int> Upset(human_file id)
        {
            return Task.Run(() => {
                human_file _File = he.human_file.Find(id.huf_id);
                _File.human_file_status = 20;
                int num = he.SaveChanges();
                return num;
            });
        }
        //薪酬发放添加
        public Task<int> Insert(salary_grant salary_)
        {
            return Task.Run(() => {
                he.salary_grant.Add(salary_);
                int num = he.SaveChanges();
                return num;
            });
        }
        //薪酬发放详细添加
        public Task<int> InsertTo(salary_grant_details salary_)
        {
            return Task.Run(() => {
                he.salary_grant_details.Add(salary_);
                int num = he.SaveChanges();
                return num;
            });
        }
        //薪酬发放未复核查询
        public Task<ResultInfo> Tofe(int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.salary_grant.Where(e => e.check_status == 0);
                var list = he.salary_grant
                    .Where(e => e.check_status == 0)
                    .OrderBy(q => q.sgr_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //薪酬发放编号查询薪酬发放表
        public Task<string> Cux(string id)
        {
            return Task.Run(() => {
                return JsonConvert.SerializeObject(he.salary_grant.Where(e => e.salary_grant_id == id));
            });
        }
        //薪酬发放编号查询薪酬详细表
        public Task<string> CuxTo(string id)
        {
            return Task.Run(() => {
                return JsonConvert.SerializeObject(he.salary_grant_details.Where(e => e.salary_grant_id == id));
            });
        }
        //薪酬编号查询人力资源表
        public Task<string> ChaX()
        {
            return Task.Run(() => {
                return JsonConvert.SerializeObject(he.human_file);
            });
        }
        //薪酬发放详细修改
        public Task<int> Update(salary_grant_details salary_)
        {
            return Task.Run(() => {
                salary_grant_details salary_1 = he.salary_grant_details.Find(salary_.grd_id);
                salary_1.bouns_sum = salary_.bouns_sum;
                salary_1.sale_sum = salary_.sale_sum;
                salary_1.deduct_sum = salary_.deduct_sum;
                salary_1.salary_paid_sum = salary_.salary_paid_sum;
                int num = he.SaveChanges();
                return num;
            });
        }
        //薪酬发放修改
        public Task<int> UpdateSe(salary_grant salary_)
        {
            return Task.Run(() => {
                salary_grant salary_11 = he.salary_grant.Find(salary_.sgr_id);
                salary_11.checker = salary_.checker;
                salary_11.check_time = salary_.check_time;
                salary_11.check_status = 1;
                salary_11.salary_paid_sum = salary_.salary_paid_sum;
                int num = he.SaveChanges();
                return num;
            });
        }
        //人力资源金额修改，状态修改
        public Task<int> UpdateTo(human_file _File)
        {
            return Task.Run(() => {
                human_file fel = he.human_file.Find(_File.huf_id);
                fel.demand_salaray_sum = _File.demand_salaray_sum;
                fel.paid_salary_sum = _File.paid_salary_sum;
                int num = he.SaveChanges();
                return num;
            });
        }
        //薪酬发放通过查询
        public Task<ResultInfo> Sequ(int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.salary_grant.Where(e => e.check_status == 1);
                var list = he.salary_grant
                    .Where(e => e.check_status == 1)
                    .OrderBy(q => q.sgr_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //薪酬发放复核通过模糊查询
        public Task<ResultInfo> QuerG0(salary_grant _File, int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.salary_grant.Where(e => e.salary_grant_id == _File.salary_grant_id
                && e.check_status == 1
                && e.third_kind_name.Contains(_File.third_kind_name)
                && e.regist_time.Date >= _File.regist_time
                && e.regist_time.Date <= _File.check_time);
                var list = he.salary_grant
                    .OrderBy(q => q.sgr_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                  .Where(e => e.salary_grant_id == _File.salary_grant_id
                && e.check_status == 1
                && e.third_kind_name.Contains(_File.third_kind_name)
                && e.regist_time.Date >= _File.regist_time
                && e.regist_time.Date <= _File.check_time)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //薪酬编号查询
        public Task<string> ChaXbh()
        {
            return Task.Run(() => {
                return JsonConvert.SerializeObject(he.salary_grant.Where(e=> e.check_status==1));
            });
        }
    }
}
